package com.rpay.common.util;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/4/18
 * @version 1.0
 */
public class StringUtil {
    private static String REGEX_NUMBER = "[0-9]*";

    /**
     * 判断字符串是否数字
     *
     * @param str 字符串
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile(REGEX_NUMBER);
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 判断字符串是否为NULL或者空字符串
     *
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否包含空格
     *
     * @param str
     * @return
     */
    public static boolean containsWhitespace(String str) {
        if (isBlank(str)) {
            return false;
        } else {
            int strLen = str.length();
            for (int i = 0; i < strLen; ++i) {
                if (Character.isWhitespace(str.charAt(i))) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 去除字符串中的空格
     *
     * @param str
     * @return
     */
    public static String trimWhitespace(String str) {
        if (isBlank(str)) {
            return str;
        }
        int len = str.length();
        StringBuffer sb = new StringBuffer(str.length());
        for (int i = 0; i < len; ++i) {
            char c = str.charAt(i);
            if (!Character.isWhitespace(c)) {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 判断字符串是否以某个字符串开头--不分大小写
     *
     * @param str
     * @param prefix
     * @return
     */
    public static boolean startsWithIgnoreCase(String str, String prefix) {
        if (str != null && prefix != null) {
            if (str.startsWith(prefix)) {
                return true;
            } else if (str.length() < prefix.length()) {
                return false;
            } else {
                String lcStr = str.substring(0, prefix.length()).toLowerCase();
                String lcPrefix = prefix.toLowerCase();
                return lcStr.equals(lcPrefix);
            }
        }
        return false;
    }

    /**
     * 判断字符串是否以某个字符串结尾--不分大小写
     *
     * @param str
     * @param suffix
     * @return
     */
    public static boolean endsWithIgnoreCase(String str, String suffix) {
        if (str != null && suffix != null) {
            if (str.endsWith(suffix)) {
                return true;
            } else if (str.length() < suffix.length()) {
                return false;
            } else {
                String lcStr = str.substring(str.length() - suffix.length()).toLowerCase();
                String lcSuffix = suffix.toLowerCase();
                return lcStr.equals(lcSuffix);
            }
        }
        return false;
    }

    public static String replace(String str, int startIndex, int length, char maskChar) {
        if (isBlank(str) || startIndex > str.length() - 1) {
            return str;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(str.substring(0, startIndex));
        int strLength = str.length() - 1 - startIndex, loopEnd = (length >= strLength ? strLength
                : length);
        for (int i = 0; i < loopEnd; i++) {
            sb.append(maskChar);
        }
        if (startIndex + length < str.length()) {
            sb.append(str.substring(startIndex + length));
        }
        return sb.toString();
    }

    /**
     * 生成验证码  6位随机数字
     *
     * @return
     */
    public static String generateVerifyCode() {
        Random random = new Random();
        String result = "";
        int size = 6;
        for (int i = 0; i < size; i++) {
            result += random.nextInt(10);
        }
        return result;
    }


    public static String getRandomString(int length) {
        //产生随机数
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        //循环length次
        for (int i = 0; i < length; i++) {
            //产生0-2个随机数，既与a-z，A-Z，0-9三种可能
            int number = random.nextInt(3);
            long result = 0;
            switch (number) {
                //如果number产生的是数字0；
                case 0:
                    //产生A-Z的ASCII码
                    result = Math.round(Math.random() * 25 + 65);
                    //将ASCII码转换成字符
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    //产生a-z的ASCII码
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
                case 2:
                    //产生0-9的数字
                    sb.append(String.valueOf(new Random().nextInt(10)));
                    break;
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getRandomString(10));
    }
}
